﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Evaluation : System.Web.UI.Page
{
    protected int EvaluationID
    {
        get
        {
            return (int)ViewState["EvaluationID"];
        }
        set
        {
            ViewState["EvaluationID"] = value;
        }
    }
    protected int CourseID
    {
        get
        {
            return (int)ViewState["CourseID"];
        }
        set
        {
            ViewState["CourseID"] = value;
        }
    }

    protected int SectionNumber
    {
        get
        {
            return (int)ViewState["SectionNumber"];
        }
        set
        {
            ViewState["SectionNumber"] = value;
        }
    }
    protected int UserID
    {
        get
        {
            return (int)Session["UserID"];
        }
        set
        {
            Session["UserID"] = value;
        }
    }

    protected EvaluationsBLL evaluationsLogic = new EvaluationsBLL();

    ILE_Global ileGlobal = new ILE_Global();

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            UsersBLL usersLogic = new UsersBLL();
            UserID = usersLogic.GetUserByUsername(HttpContext.Current.User.Identity.Name.ToString()).UserID;
        }
        catch (NullReferenceException ex)
        {
            ileGlobal.RecordError("Evaluation.aspx.cs:Page_Load:000", ex, "User Session Ended Unexpectedly", 3);
            FormsAuthentication.SignOut();
            // *SteveMsg
            string _msg = "Your Session Information has ended. You have been logged out to prevent errors.";
            ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", _msg, 2, "MyClasses.aspx");
        }



        if (!Page.IsPostBack)
        {
            EvaluationID = Convert.ToInt32(Request.QueryString["EvaluationID"]);
            CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);
            SectionNumber = Convert.ToInt32(Request.QueryString["SectionNumber"]);

            if (EvaluationID == 0 || CourseID == 0 || SectionNumber == 0)
            {
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", "Invalid access", 2, "MyClasses.aspx");
                return;
            }

            Evaluations.EvaluationsRow evaluation = evaluationsLogic.GetEvaluation(EvaluationID);
            lblEvaluationIntroduction.Text = evaluation.Introduction;
            LoadEvaluationQuestions(evaluation);
        }
        else
        {
            if (EvaluationID == 0 || CourseID == 0 || SectionNumber == 0)
            {
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", "Invalid access", 2, "MyClasses.aspx");
                return;
            }

            XDocument xDocEvaluationQuestions = new XDocument();
            Evaluations.EvaluationsRow evaluation = evaluationsLogic.GetEvaluation(EvaluationID);
            xDocEvaluationQuestions = XDocument.Parse(evaluation.Questions.ToString());

            List<XElement> questions = xDocEvaluationQuestions.Descendants("question").ToList();

            XDocument xDocEvaluationAnswers = new XDocument();
            xDocEvaluationAnswers.Declaration = new XDeclaration("1.0", "utf-8", "yes");
            xDocEvaluationAnswers.Document.Add(new XElement("evaluationanswers", new XElement("answers")));

            try
            {
                foreach (XElement question in questions)
                {
                    int QuestionID = Convert.ToInt32(question.Attribute("id").Value);

                    string type = question.Attribute("type").Value;
                    string answer = String.Empty;
                    answer = Request.Form[QuestionID.ToString()].ToString();
                    xDocEvaluationAnswers.Element("evaluationanswers").Element("answers").Add(
                        new XElement("question", new XAttribute("id", QuestionID.ToString()), new XAttribute("type", type),
                        new XElement("questiontext", question.Element("questiontext").Value),
                        new XElement("answer", answer)));
                }

            }
            catch
            {

            }

            bool result = evaluationsLogic.InsertEvaluationSubmission(EvaluationID, UserID, CourseID, SectionNumber, xDocEvaluationAnswers.ToString());

            if (result)
            {
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", "Thank you!", 1, "Class.aspx?CourseID=" + CourseID.ToString() + "&SectionNumber=" + SectionNumber.ToString());
                return;
            }
            else
            {
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", "You already have submitted the evaluation or error has occurred!!", 2, "Class.aspx?CourseID=" + CourseID.ToString() + "&SectionNumber=" + SectionNumber.ToString());
                return;
            }

        }

    }

    protected void LoadEvaluationQuestions(Evaluations.EvaluationsRow evaluation)
    {
        XDocument xDocEvaluationQuestions = new XDocument();
        xDocEvaluationQuestions = XDocument.Parse(evaluation.Questions.ToString());

        List<XElement> questions = xDocEvaluationQuestions.Descendants("question").ToList();

        lblEvaluationQuestions.Text = String.Empty;

        int counter = 1;
        foreach (XElement question in questions)
        {
            lblEvaluationQuestions.Text += counter.ToString() + ". " + question.Element("questiontext").Value.ToString() + "<br />";
            if (question.Attribute("type").Value.ToString() == "scale10")
            {
                lblEvaluationQuestions.Text += "<table style='margin-left: 20px;'><tr>";
                for (int i = 1; i <= 10; i++)
                {
                    lblEvaluationQuestions.Text += "<td style='text-align: center; width:40px;'><input type='radio' name='" + question.Attribute("id").Value.ToString() + "' value='" + i.ToString() + "' /></td>";
                }
                lblEvaluationQuestions.Text += "</tr><tr>";
                for (int i = 1; i <= 10; i++)
                {
                    if (i == 1)
                    {
                        lblEvaluationQuestions.Text += "<td style='text-align: center; vertical-align: top;'>" + i.ToString() + "<br />Strongly<br />Disagree</td>";
                    }
                    else if (i == 10)
                    {
                        lblEvaluationQuestions.Text += "<td style='text-align: center; vertical-align: top;'>" + i.ToString() + "<br />Strongly<br />Agree</td>";
                    }
                    else
                    {
                        lblEvaluationQuestions.Text += "<td style='text-align: center; vertical-align: top;'>" + i.ToString() + "</td>";
                    }
                }
                lblEvaluationQuestions.Text += "</tr></table><br /><br />";


            }
            else
            {
                lblEvaluationQuestions.Text += "<textarea name='" + question.Attribute("id").Value.ToString() + "' rows='3' cols='57'></textarea><br />";
                lblEvaluationQuestions.Text += "* Max. Characters = 1000<br /><br />";
            }
            counter++;
        }
    }
}
